package utils.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Chen Yuan
 * @create 2022-01-07  14:34
 */
public class StringUtil {
	private static final Pattern linePattern = Pattern.compile("_(\\w)");
	private static final Pattern humpPattern = Pattern.compile("[A-Z]");


	// 将所有数组替换为 指定分隔符
	public static String splitByNumber(String content, String separate) {
		Pattern pattern = Pattern.compile("[0-9]");
		Matcher matcher = pattern.matcher(content);
		return matcher.replaceAll(separate);
	}

	// 判断一个字符串是否含有数字
	public static boolean containNumber(String company) {
		Pattern p = Pattern.compile("[0-9]");
		Matcher m = p.matcher(company);
		return m.find();
	}

	/**
	 * 驼峰转下划线,最后转为大写
	 *
	 * @param str
	 * @return
	 */
	public static String humpToLine(String str, Boolean upper) {
		Matcher matcher = humpPattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
		}
		matcher.appendTail(sb);
		if (upper) {
			return sb.toString().toUpperCase();
		} else {
			return sb.toString().toLowerCase();
		}
	}

	/**
	 * 下划线转驼峰,正常输出
	 *
	 * @param str
	 * @return
	 */
	public static String lineToHump(String str) {
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	public static void main(String[] args) {
		System.out.println(StringUtil.humpToLine("stringUtil", false));
		System.out.println(StringUtil.lineToHump("clinic_registration_id"));
	}
}
